package service

import (
	"github.com/gin-gonic/gin"
	//"github.com/asaskevich/govalidator"
	//"github.com/gorilla/websocket"
	"github.com/spf13/viper"
	"go_part/models"
	"strconv"
	"fmt"
	"math/rand"
	"go_part/utils"
	//"net/http"
	"time"
  )

// GetCompanyList
// @Tags         获得企业列表
// @Success      200  {string}  json{"code","message"}
// @Router       /company/GetCompanyList [get]
func GetCompanyList(c *gin.Context) {

	data := make([]*models.CompanyBasic,10)
	data = models.GetCompanyList()

	c.JSON(200, gin.H{
		"message": data,
	  })
}

// CreateCompany
// @Tags         创建企业
// @Param name query string false "用户名"
// @Param password query string false "密码"
// @Param repassword query string false "确认密码"
// @Param logoPath query string false "确认密码"
// @Param DeviceInfo query string false "确认密码"
// @Success      200  {string}  json{"code","message"}
// @Router       /company/CreateCompany [get]
func CreateCompany(c *gin.Context) {

	user := models.CompanyBasic{}
	user.Name = c.Query("name")
	passWord := c.Query("password")
	repassWord := c.Query("repassword")
	user.LogoPath = c.Query("logoPath")
	user.DeviceInfo = c.Query("DeviceInfo")

	salt := fmt.Sprintf("%06d",rand.Int31())

	data := models.FindCompanyByName(user.Name)
	if data.Name != "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名已存在",
			"code": 404,
		  })
		return
	}

	if passWord != repassWord {
		c.JSON(-1 ,gin.H{
			"message": "两次密码不一致",
			"code": 404,
		  })
		return
	}
	//user.PassWord = passWord
	user.PassWord = utils.MakePassword(passWord,salt)
	user.Salt = salt
	user.CompanyID = strconv.Itoa(generateRandomNumber());
	user.LocalPassWord = strconv.Itoa(generateRandomNumber())+passWord;
	models.CreateCompany(user)
	c.JSON(200 ,gin.H{
		"message": "新增公司成功！",
		"data": user,
		"code": 200,
	  })
}

// FindCompanyByID
// @Tags         查找企业（根据企业ID）
// @Param CompanyID query string false "公司ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /company/FindCompanyByID [get]
func FindCompanyByID(c *gin.Context) {

	user := models.CompanyBasic{}
	user.CompanyID = c.Query("CompanyID")

	data := models.CompanyBasic{}
	data = models.FindCompanyByID(user.CompanyID)
	c.JSON(200 ,gin.H{
		"code": 200,
		"message": data,
	  })
}

// FindCompanyByIDAndPassword
// @Tags         查找企业（根据企业ID和密码）
// @Param CompanyID query string false "公司ID"
// @Param PassWord query string false "密码"
// @Success      200  {string}  json{"code","message"}
// @Router       /company/FindCompanyByIDAndPassword [get]
func FindCompanyByIDAndPassword(c *gin.Context) {

	user := models.CompanyBasic{}
	user.CompanyID = c.Query("CompanyID")
	PassWord := c.Query("PassWord")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"message": "公司不存在",
		  })
		  return
	}

	fmt.Println(data)

	flag := utils.VaildPassword(PassWord,data.Salt,data.PassWord)
	if !flag {
		c.JSON(200, gin.H{
			"code": 500,
			"message": "密码不正确",
		  })
		  return
	}

	pwd := utils.MakePassword(PassWord, data.Salt)
	data = models.FindCompanyByIDAndPassword(user.CompanyID,pwd)

	c.JSON(200 ,gin.H{
		"code": 200,
		"message": data,
	  })
}

// DeleteCompany
// @Tags         删除企业
// @Param ID query string false "公司ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /company/DeleteCompany [get]
func DeleteCompany(c *gin.Context) {

	user := models.CompanyBasic{}
	id,_ := strconv.Atoi(c.Query("ID"))
	user.ID = uint(id)

	models.DeleteCompany(user)
	c.JSON(200 ,gin.H{
		"code": 200,
		"message": "删除公司成功！",
	  })
}

// UpdateCompany
// @Tags         更新企业
// @Param id formData string false "id"
// @Param name formData string false "公司名"
// @Param LogoPath formData string false "logo路径"
// @Param DeviceInfo formData string false "描述"
// @Success      200  {string}  json{"code","message"}
// @Router       /company/UpdateCompany [post]
func UpdateCompany(c *gin.Context) {

	user := models.CompanyBasic{}
	id,_ := strconv.Atoi(c.PostForm("id"))
	user.ID = uint(id)
	user.Name = c.PostForm("name")
	user.LogoPath = c.PostForm("LogoPath")
	user.DeviceInfo = c.PostForm("DeviceInfo")

	data := models.FindCompanyByName(user.Name)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
			"code": 404,
		  })
		return
	}

	models.UpdateCompany(user)
	c.JSON(200 ,gin.H{
		"code": 200,
		"message": "更新用户成功！",
	  })
}


// InviteNumber
// @Tags         校验邀请码
// @Param name query string false "页面ID"
// @Param ParentID query string false "邀请码"
// @Success      200  {string}  json{"code","message"}
// @Router       /company/InviteNumber [get]
func InviteNumber(c *gin.Context){

	Name := c.Query("name")
	ParentID := c.Query("ParentID")

	viper.SetConfigName("app")
	viper.AddConfigPath("config")
	err := viper.ReadInConfig()
	if err!=nil {
		fmt.Println(err)
	}

	webId := viper.Get("system.webId")
	inviteId := viper.Get("system.inviteId")

	fmt.Println("Config webId: ",webId)
	fmt.Println("Config inviteId: ",inviteId)
	fmt.Println("页面ID: ",Name)
	fmt.Println("邀请码: ",ParentID)

	if Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "非法访问",
			"code": 404,
		  })
		return
	}

	if ParentID == "" {
		c.JSON(-1 ,gin.H{
			"message": "非法访问",
			"code": 404,
		  })
		return
	}

	if Name != webId {
		c.JSON(-1 ,gin.H{
			"message": "非法访问,请通过正确途径访问",
			"code": 404,
		  })
		return
	}

	if ParentID == inviteId{
		c.JSON(-1 ,gin.H{
			"message": "成功",
			"code": 200,
		  })
		return
	}
	
	c.JSON(-1 ,gin.H{
		"message": "非法访问,请通过正确途径访问",
		"code": 404,
	  })
	return

}

// GetLevelTypeList
// @Tags         获得公司层级列表
// @Success      200  {string}  json{"code","message"}
// @Router       /company/GetLevelTypeList [get]
func GetLevelTypeList(c *gin.Context) {

	data := make([]*models.LevelType,10)
	data = models.GetLevelTypeList()

	c.JSON(200, gin.H{
		"message": data,
		"code": 200,
	  })
}

// CreateLevelType
// @Tags         创建层级类型
// @Param name query string false "层级类型名"
// @Param ParentTypeID query string false "父级ID"
// @Param CompanyID query string false "公司ID"
// @Param DeviceInfo query string false "描述信息"
// @Success      200  {string}  json{"code","message"}
// @Router       /company/CreateLevelType [get]
func CreateLevelType(c *gin.Context) {

	user := models.LevelType{}
	user.Name = c.Query("name")
	user.ParentTypeID = c.Query("ParentTypeID")
	user.CompanyID = c.Query("CompanyID")
	user.DeviceInfo = c.Query("DeviceInfo")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
			"code": 404,
		  })
		return
	}

	data1 := models.FindLevelTypeByName(user.Name,user.CompanyID)
	if data1.Name != "" {
		c.JSON(-1 ,gin.H{
			"message": "层级名已存在",
			"code": 404,
		  })
		return
	}

	data2 := models.FindLevelTypeByIDAndCompanyID(user.ParentTypeID,user.CompanyID)
	if data2.Name == "" {
		if user.ParentTypeID != user.CompanyID {
			c.JSON(-1 ,gin.H{
				"message": "父层级不存在",
				"code": 404,
			})
			return
		}
	}

	user.LevelTypeID = strconv.Itoa(generateRandomNumber());
	models.CreateLevelType(user)
	c.JSON(200 ,gin.H{
		"message": "新增层级成功！",
		"code": 200,
	  })
}

// FindLevelTypeByID
// @Tags         查找层级类型（根据层级ID）
// @Param CompanyID query string false "公司ID"
// @Param LevelTypeID query string false "层级自定义ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /company/FindLevelTypeByID [get]
func FindLevelTypeByID(c *gin.Context) {

	user := models.LevelType{}
	user.CompanyID = c.Query("CompanyID")
	user.LevelTypeID = c.Query("LevelTypeID")

	data := models.LevelType{}
	data = models.FindLevelTypeByIDAndCompanyID(user.LevelTypeID,user.CompanyID)
	c.JSON(200 ,gin.H{
		"code": 200,
		"message": data,
	  })
}

// FindLevelTypeByCompanyID
// @Tags         查找层级类型（根据公司ID）
// @Param CompanyID query string false "公司ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /company/FindLevelTypeByCompanyID [get]
func FindLevelTypeByCompanyID(c *gin.Context) {

	user := models.LevelType{}
	user.CompanyID = c.Query("CompanyID")

	data := make([]*models.LevelType,10)
	data = models.FindLevelTypeByCompanyID(user.CompanyID)
	c.JSON(200 ,gin.H{
		"code": 200,
		"message": data,
	  })
}

// DeleteLevelType
// @Tags         删除层级类型
// @Param id query string false "层级ID"
// @Param CompanyID query string false "公司ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /company/DeleteLevelType [get]
func DeleteLevelType(c *gin.Context) {

	user := models.LevelType{}
	id,_ := strconv.Atoi(c.Query("id"))
	user.ID = uint(id)
	user.CompanyID = c.Query("CompanyID")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
			"code": 404,
		  })
		return
	}

	models.DeleteLevelType(user)
	c.JSON(200 ,gin.H{
		"code": 200,
		"message": "删除层级类型成功！",
	  })
}

// UpdateLevelType
// @Tags         更新层级类型
// @Param id formData string false "id"
// @Param name formData string false "层级类型名称"
// @Param CompanyID formData string false "公司ID"
// @Param DeviceInfo formData string false "描述"
// @Success      200  {string}  json{"code","message"}
// @Router       /company/UpdateLevelType [post]
func UpdateLevelType(c *gin.Context) {

	user := models.LevelType{}
	id,_ := strconv.Atoi(c.PostForm("id"))
	user.ID = uint(id)
	user.Name = c.PostForm("name")
	user.LevelTypeID = c.PostForm("LevelTypeID")
	user.DeviceInfo = c.PostForm("DeviceInfo")
	user.CompanyID = c.PostForm("CompanyID")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
			"code": 404,
		  })
		return
	}

	data1 := models.FindLevelTypeByID(user.ID,user.CompanyID)
	if data1.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "层级名不存在",
			"code": 404,
		  })
		return
	}

	models.UpdateLevelType(user)
	c.JSON(200 ,gin.H{
		"code": 200,
		"message": "更新层级成功！",
	  })
}

// GetDeparkmentList
// @Tags         获得公司部门列表
// @Success      200  {string}  json{"code","message"}
// @Router       /company/GetDeparkmentList [get]
func GetDeparkmentList(c *gin.Context) {

	data := make([]*models.DeparkmentBasic,10)
	data = models.GetDeparkmentList()

	c.JSON(200, gin.H{
		"message": data,
		"code": 200,
	  })
}

// CreateDeparkment
// @Tags         创建公司部门
// @Param name query string false "部门名称"
// @Param CompanyID query string false "公司ID"
// @Param DeviceInfo query string false "描述"
// @Success      200  {string}  json{"code","message"}
// @Router       /company/CreateDeparkment [get]
func CreateDeparkment(c *gin.Context) {

	user := models.DeparkmentBasic{}
	user.Name = c.Query("name")
	user.CompanyID = c.Query("CompanyID")
	user.DeviceInfo = c.Query("DeviceInfo")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
			"code": 404,
		  })
		return
	}

	data1 := models.FindDeparkmentBasicByName(user.Name,user.CompanyID)
	if data1.Name != "" {
		c.JSON(-1 ,gin.H{
			"message": "部门名已存在",
			"code": 404,
		  })
		return
	}

	user.DeparkmentID = strconv.Itoa(generateRandomNumber());
	models.CreateDeparkmentBasic(user)
	c.JSON(200 ,gin.H{
		"message": "新增部门成功！",
		"code": 200,
	  })
}

// FindDeparkmentByCompanyID
// @Tags         查找部门（根据公司ID）
// @Param CompanyID query string false "公司ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /company/FindDeparkmentByCompanyID [get]
func FindDeparkmentByCompanyID(c *gin.Context) {

	user := models.DeparkmentBasic{}
	user.CompanyID = c.Query("CompanyID")

	data := make([]*models.DeparkmentBasic,100)
	data = models.FindDeparkmentByCompanyID(user.CompanyID)
	c.JSON(200 ,gin.H{
		"code": 200,
		"message": data,
	  })
}

// DeleteDeparkment
// @Tags         删除公司部门
// @Param id query string false "id"
// @Param CompanyID query string false "公司ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /company/DeleteDeparkment [get]
func DeleteDeparkment(c *gin.Context) {

	user := models.DeparkmentBasic{}
	id,_ := strconv.Atoi(c.Query("id"))
	user.ID = uint(id)
	user.CompanyID = c.Query("CompanyID")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
			"code": 404,
		  })
		return
	}

	models.DeleteDeparkmentBasic(user)
	c.JSON(200 ,gin.H{
		"code": 200,
		"message": "删除公司成功！",
	  })
}

// UpdateDeparkment
// @Tags         更新公司部门
// @Param id formData string false "id"
// @Param name formData string false "部门名称"
// @Param CompanyID formData string false "公司ID"
// @Param DeviceInfo formData string false "描述"
// @Success      200  {string}  json{"code","message"}
// @Router       /company/UpdateDeparkment [post]
func UpdateDeparkment(c *gin.Context) {

	user := models.DeparkmentBasic{}
	id,_ := strconv.Atoi(c.PostForm("id"))
	user.ID = uint(id)
	user.Name = c.PostForm("name")
	user.CompanyID = c.PostForm("CompanyID")
	user.DeviceInfo = c.PostForm("DeviceInfo")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
			"code": 404,
		  })
		return
	}

	data1 := models.FindDeparkmentBasicByID(user.ID)
	if data1.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "部门不存在",
			"code": 404,
		  })
		return
	}

	models.UpdateDeparkmentBasic(user)
	c.JSON(200 ,gin.H{
		"code": 200,
		"message": "更新部门成功！",
	  })
}

// GetLevelLocalList
// @Tags         获得公司区域层级列表
// @Success      200  {string}  json{"code","message"}
// @Router       /company/GetLevelLocalList [get]
func GetLevelLocalList(c *gin.Context) {

	data := make([]*models.LevelLocal,10)
	data = models.GetLevelLocalList()

	c.JSON(200, gin.H{
		"message": data,
		"code": 200,
	  })
}

// CreateLevelLocal
// @Tags         创建公司区域层级
// @Param name query string false "区域名称"
// @Param CompanyID query string false "公司ID"
// @Param LevelTypeID query string false "层级类型ID"
// @Param ParentLocalID query string false "父层级ID"
// @Param DeviceInfo query string false "描述"
// @Success      200  {string}  json{"code","message"}
// @Router       /company/CreateLevelLocal [get]
func CreateLevelLocal(c *gin.Context) {

	user := models.LevelLocal{}
	user.Name = c.Query("name")
	user.CompanyID = c.Query("CompanyID")
	user.LevelTypeID = c.Query("LevelTypeID")
	user.ParentLocalID = c.Query("ParentLocalID")
	user.DeviceInfo = c.Query("DeviceInfo")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	data1 := models.FindLevelTypeByIDAndCompanyID(user.LevelTypeID,user.CompanyID)
	if data1.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "层级名不存在",
		  })
		return
	}

	data2 := models.FindLevelLocalByIDAndCompanyID(user.ParentLocalID,user.CompanyID)
	if data2.Name == "" {
		if user.ParentLocalID != user.CompanyID {
			c.JSON(-1 ,gin.H{
				"message": "父层级不存在",
			})
			return
		}
	}else {
		//检查是不是等于 子层级类型的父层级类型
		if data2.LevelTypeID != data1.ParentTypeID {
			c.JSON(-1 ,gin.H{
				"message": "父层级类型不正确",
			})
			return			
		}
	}

	data3 := models.FindLevelLocalByName(user.Name)
	if data3.Name != "" {
		c.JSON(-1 ,gin.H{
			"message": "区域名已存在",
		  })
		return
	}

	user.LevelLocalID = strconv.Itoa(generateRandomNumber());
	models.CreateLevelLocal(user)
	c.JSON(200 ,gin.H{
		"message": "新增区域成功！",
	  })
}

// FindLevelLocalByTypeIDAndCompanyID
// @Tags         查找区域（根据层级ID）
// @Param CompanyID query string false "公司ID"
// @Param LevelTypeID query string false "层级自定义ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /company/FindLevelLocalByTypeIDAndCompanyID [get]
func FindLevelLocalByTypeIDAndCompanyID(c *gin.Context) {
 
	user := models.LevelLocal{}
	user.CompanyID = c.Query("CompanyID")
	user.LevelTypeID = c.Query("LevelTypeID")

	data := make([]*models.LevelLocal,100)
	data = models.FindLevelLocalByTypeIDAndCompanyID(user.LevelTypeID,user.CompanyID)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": data,
	  })
}

// DeleteLevelLocal
// @Tags         删除公司区域层级
// @Param id query string false "id"
// @Param CompanyID query string false "公司ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /company/DeleteLevelLocal [get]
func DeleteLevelLocal(c *gin.Context) {

	user := models.LevelLocal{}
	id,_ := strconv.Atoi(c.Query("id"))
	user.ID = uint(id)
	user.CompanyID = c.Query("CompanyID")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	models.DeleteLevelLocal(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "删除区域成功！",
	  })
}

// UpdateLevelLocal
// @Tags         更新公司区域层级
// @Param id formData string false "id"
// @Param name formData string false "区域名称"
// @Param CompanyID formData string false "公司ID"
// @Param DeviceInfo formData string false "描述"
// @Success      200  {string}  json{"code","message"}
// @Router       /company/UpdateLevelLocal [post]
func UpdateLevelLocal(c *gin.Context) {

	user := models.LevelLocal{}
	id,_ := strconv.Atoi(c.PostForm("id"))
	user.ID = uint(id)
	user.Name = c.PostForm("name")
	user.CompanyID = c.PostForm("CompanyID")
	user.DeviceInfo = c.PostForm("DeviceInfo")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	models.UpdateLevelLocal(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "更新区域成功！",
	  })
}

// GetLevelDeviceList
// @Tags         获得公司区域设备列表
// @Success      200  {string}  json{"code","message"}
// @Router       /company/GetLevelDeviceList [get]
func GetLevelDeviceList(c *gin.Context) {

	data := make([]*models.LevelDevice,10)
	data = models.GetLevelDeviceList()

	c.JSON(200, gin.H{
		"message": data,
	  })
}

// CreateLevelDevice
// @Tags         创建公司区域设备
// @Param name query string false "设备名称"
// @Param CompanyID query string false "公司ID"
// @Param LevelLocalID query string false "所属区域ID"
// @Param DeviceInfo query string false "描述"
// @Param DeviceTypeID query string false "设备类型"
// @Param Longitude query string false "经度"
// @Param Latitude query string false "纬度"
// @Success      200  {string}  json{"code","message"}
// @Router       /company/CreateLevelDevice [get]
func CreateLevelDevice(c *gin.Context) {

	user := models.LevelDevice{}
	user.Name = c.Query("name")
	user.CompanyID = c.Query("CompanyID")
	user.LevelLocalID = c.Query("LevelLocalID")
	user.DeviceInfo = c.Query("DeviceInfo")
	user.DeviceTypeID = c.Query("DeviceTypeInfo")
	user.Longitude = c.Query("Longitude")
	user.Latitude = c.Query("Latitude")
	user.IsLogout = false

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	data1 := models.FindLevelDeviceByName(user.Name,user.CompanyID)
	if data1.Name != "" {
		c.JSON(-1 ,gin.H{
			"message": "设备名已存在",
		  })
		return
	}

	user.DeviceID = strconv.Itoa(generateRandomNumber());
	models.CreateLevelDevice(user)
	c.JSON(200 ,gin.H{
		"message": "新增设备成功！",
	  })
}

// FindLevelDeviceByTypeIDAndCompanyID
// @Tags         查找区域设备（根据层级ID）
// @Param CompanyID query string false "公司ID"
// @Param LevelLocalID query string false "层级自定义ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /company/FindLevelDeviceByTypeIDAndCompanyID [get]
func FindLevelDeviceByTypeIDAndCompanyID(c *gin.Context) {
 
	user := models.LevelDevice{}
	user.CompanyID = c.Query("CompanyID")
	user.LevelLocalID = c.Query("LevelLocalID")

	data := make([]*models.LevelDevice,100)
	data = models.FindLevelDeviceByTypeIDAndCompanyID(user.LevelLocalID,user.CompanyID)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": data,
	  })
}

// DeleteLevelDevice
// @Tags         删除公司区域设备
// @Param id query string false "id"
// @Param CompanyID query string false "公司ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /company/DeleteLevelDevice [get]
func DeleteLevelDevice(c *gin.Context) {

	user := models.LevelDevice{}
	id,_ := strconv.Atoi(c.Query("id"))
	user.ID = uint(id)
	user.CompanyID = c.Query("CompanyID")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	models.DeleteLevelDevice(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "删除设备成功！",
	  })
}

// UpdateLevelDevice
// @Tags         更新公司区域设备
// @Param id formData string false "id"
// @Param name formData string false "设备名称"
// @Param CompanyID formData string false "公司ID"
// @Param DeviceInfo formData string false "描述"
// @Param DeviceTypeID formData string false "设备类型"
// @Param Longitude formData string false "经度"
// @Param Latitude formData string false "纬度"
// @Success      200  {string}  json{"code","message"}
// @Router       /company/UpdateLevelDevice [post]
func UpdateLevelDevice(c *gin.Context) {

	user := models.LevelDevice{}
	id,_ := strconv.Atoi(c.PostForm("id"))
	user.ID = uint(id)
	user.Name = c.PostForm("name")
	user.CompanyID = c.PostForm("CompanyID")
	user.DeviceInfo = c.PostForm("DeviceInfo")
	user.DeviceTypeID = c.PostForm("DeviceTypeID")
	user.Longitude = c.PostForm("Longitude")
	user.Latitude = c.PostForm("Latitude")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	models.UpdateLevelDevice(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "更新设备成功！",
	  })
}

func generateRandomNumber() int {
    rand.Seed(time.Now().UnixNano())
    // 生成10000000到99999999之间的随机数，保证是8位数
    return rand.Intn(90000000) + 10000000
}